home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 1
/
Cream of the Crop 1.iso
/
PROGRAM
/
SNIP0492.ARJ
/
DAYLEN.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-09-24
|
10KB
|
241 lines
/*
DAYLEN.C - computes the length of the day at any date and latitude
Paul Schlyter, 1989-08-16
(c) Paul Schlyter, 1989
This program may be used by anyone for any purpose, iff:
1. it is not being sold for profit
2. this notice is not removed
*/
#include <stdio.h>
#include <math.h>
/* A macro to compute the number of days elapsed since 2000 Jan 0.0 */
/* (which is equal to 1999 Dec 31, 0h UT) */
#define days_since_2000_Jan_0(y,m,d) \
(367L*(y)-((7*((y)+(((m)+9)/12)))/4)+((275*(m))/9)+(d)-730530L)
/* Some conversion factors between radians and degrees */
#ifndef PI
#define PI 3.1415926535897932384
#endif
#define RADEG ( 180.0 / PI )
#define DEGRAD ( PI / 180.0 )
/* The trigonometric functions in degrees */
#define sind(x) sin((x)*DEGRAD)
#define cosd(x) cos((x)*DEGRAD)
#define tand(x) tan((x)*DEGRAD)
#define atand(x) (RADEG*atan(x))
#define asind(x) (RADEG*asin(x))
#define acosd(x) (RADEG*acos(x))
#define atan2d(y,x) (RADEG*atan2(y,x))
/* Following are some macros around the "workhorse" function __daylen__ */
/* They mainly fill in the desired values for the reference altitude */
/* below the horizon, and also selects whether this altitude should */
/* refer to the Sun's center or its upper limb. */
/* This macro computes the length of the day, from sunrise to sunset. */
/* Sunrise/set is considered to occur when the Sun's upper limb is */
/* 35 arc minutes below the horizon (this accounts for the refraction */
/* of the Earth's atmosphere). */
#define day_length(year,month,day,lon,lat) \
__daylen__( year, month, day, lon, lat, -35.0/60.0, 1 )
/* This macro computes the length of the day, including civil twilight. */
/* Civil twilight starts/ends when the Sun's center is 6 degrees below */
/* the horizon. */
#define day_civil_twilight_length(year,month,day,lon,lat) \
__daylen__( year, month, day, lon, lat, -6.0, 0 )
/* This macro computes the length of the day, incl. nautical twilight. */
/* Nautical twilight starts/ends when the Sun's center is 12 degrees */
/* below the horizon. */
#define day_nautical_twilight_length(year,month,day,lon,lat) \
__daylen__( year, month, day, lon, lat, -12.0, 0 )
/* This macro computes the length of the day, incl. astronomical twilight. */
/* Astronomical twilight starts/ends when the Sun's center is 18 degrees */
/* below the horizon. */
#define day_astronomical_twilight_length(year,month,day,lon,lat) \
__daylen__( year, month, day, lon, lat, -18.0, 0 )
/* Function prototypes */
double __daylen__( int year, int month, int day, double lon, double lat,
double altit, int upper_limb );
void sunpos( double d, double *lon, double *r );
double revolution( double x );
/* A small test program */
void main(void)
{
int year,month,day;
double lon, lat;
double daylen, civlen, nautlen, astrlen;
printf( "Longitude (+ is east) and latitude (+ is north) : " );
scanf( "%lf %lf", &lon, &lat );
for(;;)
{
printf( "Input date ( yyyy mm dd ) (ctrl-C exits): " );
scanf( "%d %d %d", &year, &month, &day );
daylen = day_length(year,month,day,lon,lat);
civlen = day_civil_twilight_length(year,month,day,lon,lat);
nautlen = day_nautical_twilight_length(year,month,day,lon,lat);
astrlen = day_astronomical_twilight_length(year,month,day,
lon,lat);
printf( "Day length: %5.2f hours\n", daylen );
printf( "With civil twilight %5.2f hours\n", civlen );
printf( "With nautical twilight %5.2f hours\n", nautlen );
printf( "With astronomical twilight %5.2f hours\n", astrlen );
printf( "Length of twilight: civil %5.2f hours\n",
(civlen-daylen)/2.0);
printf( " nautical %5.2f hours\n",
(nautlen-daylen)/2.0);
printf( " astronomical %5.2f hours\n",
(astrlen-daylen)/2.0);
}
}
/* The "workhorse" function */
double __daylen__( int year, int month, int day, double lon, double lat,
double altit, int upper_limb )
/**********************************************************************/
/* Note: year,month,date = calendar date, 1801-2099 only. */
/* Eastern longitude positive, Western longitude negative */
/* Northern latitude positive, Southern latitude negative */
/* The longitude value is not critical. Set it to the correct */
/* longitude if you're picky, otherwise set to to, say, 0.0 */
/* The latitude however IS critical - be sure to get it correct */
/* altit = the latitude where the Sun should cross */
/* Set to -35/60 degrees for rise/set, -6 degrees */
/* for civil, -12 degrees for nautical and -18 */
/* degrees for astronomical twilight. */
/* upper_limb: non-zero -> upper limb, zero -> center */
/* Set to non-zero (e.g. 1) when computing day length */
/* and to zero when computing day+twilight length. */
/**********************************************************************/
{
double d, /* Days since 2000 Jan 0.0 (negative before) */
obl_ecl, /* Obliquity (inclination) of Earth's axis */
sr, /* Solar distance, astronomical units */
slon, /* True solar longitude */
sin_sdecl, /* Sine of Sun's declination */
cos_sdecl, /* Cosine of Sun's declination */
sradius, /* Sun's apparent radius */
t; /* Diurnal arc */
/* Compute d of 12h local mean solar time */
d = days_since_2000_Jan_0(year,month,day) - lon/360.0;
/* Compute obliquity of ecliptic (inclination of Earth's axis */
obl_ecl = 23.4393 - 3.563E-7 * d;
/* Compute Sun's position */
sunpos( d, &slon, &sr );
/* Compute sine and cosine of Sun's declination */
sin_sdecl = sind(obl_ecl) * sind(slon);
cos_sdecl = sqrt( 1.0 - sin_sdecl * sin_sdecl );
/* Compute the Sun's apparent radius, degrees */
sradius = 0.2666 / sr;
/* Do correction to upper limb, if necessary */
if ( upper_limb )
altit -= sradius;
/* Compute the diurnal arc that the Sun traverses to reach */
/* the specified altitide altit: */
{
double cost;
cost = ( sind(altit) - sind(lat) * sin_sdecl ) /
( cosd(lat) * cos_sdecl );
if ( cost >= 1.0 )
t = 0.0; /* Sun always below altit */
else if ( cost <= -1.0 )
t = 24.0; /* Sun always above altit */
else t = (2.0/15.0) * acosd(cost); /* The diurnal arc, hours */
}
return t;
}
/* This function computes the Sun's position at any instant */
void sunpos( double d, double *lon, double *r )
/******************************************************/
/* Computes the Sun's ecliptic longitude and distance */
/* at an instant given in d, number of days since */
/* 2000 Jan 0.0. The Sun's ecliptic latitude is not */
/* computed, since it's always very near 0. */
/******************************************************/
{
double M, /* Mean anomaly of the Sun */
w, /* Mean longitude of perihelion */
/* Note: Sun's mean longitude = M + w */
e, /* Eccentricity of Earth's orbit */
E, /* Eccentric anomaly */
x, y, /* x, y coordinates in orbit */
v; /* True anomaly */
/* Compute mean elements */
M = revolution( 356.0470 + 0.9856002585 * d );
w = 282.9404 + 4.70935E-5 * d;
e = 0.016709 - 1.151E-9 * d;
/* Compute true longitude and radius vector */
E = M + e * RADEG * sind(M) * ( 1.0 + e * cosd(M) );
x = cosd(E) - e;
y = sqrt( 1.0 - e*e ) * sind(E);
*r = sqrt( x*x + y*y ); /* Solar distance */
v = atan2d( y, x ); /* True anomaly */
*lon = v + w; /* True solar longitude */
if ( *lon >= 360.0 )
*lon -= 360.0; /* Make it 0..360 degrees */
}
/******************************************************************/
/* This function reduces any angle to within the first revolution */
/* by subtracting or adding even multiples of 360.0 until the */
/* result is >= 0.0 and < 360.0 */
/******************************************************************/
#define INV360 ( 1.0 / 360.0 )
double revolution( double x )
/*****************************************/
/* Reduce angle to within 0..360 degrees */
/*****************************************/
{
return( x - 360.0 * floor( x * INV360 ) );
}